<?php
/**
* Handles updating reservations with new site coordinators
* @package phpScheduleIt
*
* License: GPL, see LICENSE
*/

$basedir = dirname(__FILE__);
include_once($basedir . '/lib/Template.class.php');
include_once($basedir . '/lib/Reservation.class.php');
include_once($basedir . '/lib/AdditionalResource.class.php');
include_once($basedir . 'lib/User.class.php');
include_once($basedir . '/lib/Reservation.class.php');

$t = new Template(translate('My Control Panel'));
$db = new DBEngine();

$t->printHTMLHeader();
$t->printWelcome();
$fn = isset($_POST['fn']) ? $_POST['fn'] : (isset($_GET['fn']) ? $_GET['fn'] : '');	// Set function

$user = new User(Auth::getCurrentID());

if (!validate_function($user, $fn)) {
    CmnFns::do_error_box(translate('This is only accessable to the administrator') . '<br />'
        . '<a href="ctrlpnl.php">' . translate('Back to My Control Panel') . '</a>');
}

$t->startMain();

$res = New Reservation();
$resource = New AdditionalResource();

$reservations = $res->db->get_all_resids();
//print_r($reservations);

echo '<br>' . 'Searching all meetings to update site coordinators:' . '<br>';


foreach ($reservations as $current) {
  $users =  $res->db->get_res_users($current['resid']);
  $sites = $res->db->get_sup_resources($current['resid']);
  foreach ($sites as $current_site) {
    $found = false;
    $founduser = null;
    $coord = $current_site['sitecoord_id'];
    foreach ($users as $current_user){
      if ($coord == $current_user['memberid']) {
        $found = true;
      }
    }
    if ((!$found) &&($coord != '')) {
      $coord_user = New User($coord);
      $site_coord = array();
      $site_coord[$coord] = $coord_user->get_email();
      $reservation = new Reservation($current['resid']);
      $reservation->origstartdate = $reservation->start_date;  ///////
      $reservation->origstarttime = $reservation->get_start();
      $reservation->origendtime = $reservation->get_end();
      
      $userlist = array();
      foreach ($users as $current_user) {
        $userlist[$current_user['memberid']] = $current_user['email'];
      }
      
  //    mod_res($users_to_invite, $users_to_remove, $unchanged_users, $resources_to_add, $resources_to_remove, $del, $mod_recur)
      $reservation->mod_res($site_coord, null, $userlist, null, null, false, null);
    }
  }
  
}

echo '<br>' . 'Finished updating meetings.' . '<br>';
echo 'Return to ';
echo '<a href="ctrlpnl.php">My Control Panel</a>';


function validate_function(&$user, $function) {
	if (Auth::isAdmin() || $user->get_isadmin()) {
		return true;
	}

	if ( $function == 'editPerms' || $function == 'resetPass' ) {
		return $user->is_group_admin();
	}

	return false;
}
?>